/**
 * 
 */
package cn.edu.nju.ws.sview.wyge.relations.adataset.relations;

import cn.edu.nju.ws.sview.cache.URIIDCache;

/**
 * @author Weiyi Ge (geweiyi@gmail.com) 2012-8-15
 * 
 */
public class TriplePattern {
	private String s;
	private int p;
	private String o;
	private String s_text;
	private String p_text;
	private String o_text;

	public TriplePattern(String s, int p, String o) {
		this.s = s;
		this.p = p;
		this.o = o;
		try {
			s_text = s;
			if (s_text.startsWith("u")) {
				s_text = URIIDCache.getInstance().getURI(
						Integer.parseInt(s_text.substring(1)));
			}
			p_text = URIIDCache.getInstance().getURI(p);
			o_text = o;
			if (o_text.startsWith("u")) {
				o_text = URIIDCache.getInstance().getURI(
						Integer.parseInt(o_text.substring(1)));
			} else if (o_text.startsWith("l")) {
				o_text = o_text.substring(1);
			}
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Throwable e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public TriplePattern(String s, int p, String o, String s_text,
			String p_text, String o_text) {
		this.s = s;
		this.p = p;
		this.o = o;
		this.s_text = s_text;
		this.p_text = p_text;
		this.o_text = o_text;
	}

	public static TriplePattern getTriple(String s, int p, String o) {
		String s_text = "";
		String p_text = "";
		String o_text = "";
		try {
			s_text = s;
			if (s_text.startsWith("u")) {
				s_text = URIIDCache.getInstance().getURI(
						Integer.parseInt(s_text.substring(1)));
			}
			p_text = URIIDCache.getInstance().getURI(p);
			o_text = o;
			if (o_text.startsWith("u")) {
				o_text = URIIDCache.getInstance().getURI(
						Integer.parseInt(o_text.substring(1)));
			} else if (o_text.startsWith("l")) {
				o_text = o_text.substring(1);
			}
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Throwable e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return new TriplePattern(s, p, o, s_text, p_text, o_text);
	}

	public static TriplePattern getTriple(String s_code, String p_code,
			String o_code) {
		String s = "";
		int p = 0;
		String o = "";
		String s_text = "";
		String p_text = "";
		String o_text = "";
		if (s_code.startsWith("u")) {
			s_text = s_code.substring(1);
			int uid = 0;
			try {
				uid = URIIDCache.getInstance().getURIID(s_text, false);
			} catch (Throwable e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			s = "u" + uid;
		} else if (s_code.startsWith("b")) {
			s = s_code;
			s_text = s_code;
		} else if (s_code.startsWith("l")) {
			s = s_code;
			s_text = s.substring(1);
		} else if (s_code.startsWith("v")) {
			s = s_code;
			s_text = s_code;
		}

		if (p_code.startsWith("u")) {
			p_text = p_code.substring(1);
			int uid = 0;
			try {
				uid = URIIDCache.getInstance().getURIID(p_text, false);
			} catch (Throwable e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			p = uid;
		}

		if (o_code.startsWith("u")) {
			o_text = o_code.substring(1);
			int uid = 0;
			try {
				uid = URIIDCache.getInstance().getURIID(o_text, false);
			} catch (Throwable e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			o = "u" + uid;
		} else if (o_code.startsWith("b")) {
			o = o_code;
			o_text = o_code;
		} else if (o_code.startsWith("l")) {
			o = o_code;
			o_text = o.substring(1);
		} else if (o_code.startsWith("v")) {
			o = o_code;
			o_text = o_code;
		}

		return new TriplePattern(s, p, o, s_text, p_text, o_text);
	}

	public void bindVariable(String v, String value) {
		if (s.startsWith("v") && s.equals(v)) {
			s = value;
			s_text = s;
			try {
				if (s_text.startsWith("u")) {
					s_text = URIIDCache.getInstance().getURI(
							Integer.parseInt(s_text.substring(1)));
				}
			} catch (Throwable e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (o.startsWith("v") && o.equals(v)) {
			o = value;
			o_text = o;
			try {
				if (o_text.startsWith("u")) {
					o_text = URIIDCache.getInstance().getURI(
							Integer.parseInt(o_text.substring(1)));
				} else if (o_text.startsWith("l")) {
					o_text = o_text.substring(1);
				}
			} catch (Throwable e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public String getSubject() {
		return s;
	}

	public int getProperty() {
		return p;
	}

	public String getObject() {
		return o;
	}

	public String getSubjectText() {
		return s_text;
	}

	public String getPropertyText() {
		return p_text;
	}

	public String getObjectText() {
		return o_text;
	}

	public String toString() {
		return "(" + s + ", " + p + ", " + o + ")";
	}
}
